package com.goodluck.goodluckim.sdk.constant;

import com.goodluck.goodluckim.db.model.CSLogin;
import com.goodluck.goodluckim.db.model.CSMessage;
import com.goodluck.goodluckim.db.model.CSPing;
import com.goodluck.goodluckim.db.model.IMMessage;
import com.goodluck.goodluckim.db.model.MessageStatusChange;
import com.goodluck.goodluckim.db.model.SCLoginResult;
import com.goodluck.goodluckim.db.model.SCPingResult;
import com.goodluck.goodluckim.utils.GsonUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wyj on 2018/9/7.
 */

public class MsgActions {

    /**
     * 登陆
     */
    public static final String CS_LOGIN = "cs_login";

    /**
     * 登陆返回消息
     */
    public static final String SC_LOGIN_RESULT = "sc_login";

    /**
     * 发送消息
     */
    public static final String CS_MESSAGE = "cs_message";
    /**
     * 接收消息
     */
    public static final String SC_MESSAGE_RESULT = "sc_message";
    /**
     * 消息状态变更
     */
    public static final String SC_MESSAGE_STATUS_CHANGE = "sc_message_status_change";

    /**
     * 发送心跳包
     */
    public static final String CS_PING = "cs_ping";
    /**
     * 接收心跳包
     */
    public static final String SC_PING_RESULT = "sc_ping";


    /**
     * 消息CLASS与消息action的对应关系<消息class, 消息action>
     */
    private static final Map<Class<?>, String> classToAction = new HashMap<>();

    static {
        //初始化消息CLASS与消息action的对应关系
        initClassToAction();
    }

    /**
     * 获取消息action
     *
     * @param clazz
     * @return
     */
    public static String getActionByClass(Class<?> clazz) {
        return classToAction.get(clazz);
    }

    /**
     * 初始化消息CLASS与消息action的对应关系
     */
    private static void initClassToAction() {
        classToAction.put(CSLogin.class, CS_LOGIN);
        classToAction.put(SCLoginResult.class, SC_LOGIN_RESULT);
        classToAction.put(CSMessage.class, CS_MESSAGE);
        classToAction.put(CSPing.class, CS_PING);
        classToAction.put(IMMessage.class, SC_MESSAGE_RESULT);
        classToAction.put(MessageStatusChange.class, SC_MESSAGE_STATUS_CHANGE);
        classToAction.put(SCPingResult.class, SC_PING_RESULT);
    }

    /**
     * 根据消息action解析消息
     */
    public static Object parseFrom(String action, String json) {
        if (SC_LOGIN_RESULT.equals(action)) {
            return GsonUtil.getInstance().fromJson(json, SCLoginResult.class);
        } else if (SC_MESSAGE_RESULT.equals(action)) {
            return GsonUtil.getInstance().fromJson(json, IMMessage.class);
        } else if (SC_PING_RESULT.equals(action)) {
            return GsonUtil.getInstance().fromJson(json, SCPingResult.class);
        } else {
            return null;
        }
    }
}
